#!/bin/sh

#H1---------R1------------R2--------H2

#		   R3

ip netns add H1
ip netns add R1
ip netns add R2
ip netns add R3
ip netns add H2

#H1
ip link add name H1_R1 type veth peer name R1_H1
ip link set H1_R1 netns H1
ip netns exec H1 bash -c \
'
	ip link set lo up
	ip link set H1_R1 up

	ip addr add 192.168.0.1/24 dev H1_R1
	ip route add 10.0.0.0/24 via 192.168.0.254
'

#R1
ip link add name R1_R2 type veth peer name R2_R1
ip link add name R1_R3 type veth peer name R3_R1
ip link set R1_H1 netns R1
ip link set R1_R2 netns R1
ip link set R1_R3 netns R1
ip netns exec R1 bash -c \
'
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R1_H1.seg6_enabled=1
	sysctl net.ipv6.conf.R1_R2.seg6_enabled=1

	ip link set lo up
	ip link set R1_H1 up
	ip link set R1_R2 up
	ip link set R1_R3 up

	ip addr add 192.168.0.254/24 dev R1_H1
	ip addr add 2001:db8:1::12/64 dev R1_R2
	ip addr add 2001:db8:2::13/64 dev R1_R3
	ip addr add fe80::1/64 dev R1_H1
	ip addr add fe80::1/64 dev R1_R2
	ip addr add fe80::1/64 dev R1_R3

	ip route add A2::2 via fe80::2 dev R1_R2
	ip -6 route add A1::1 encap seg6local action End.DX4 nh4 192.168.0.1 dev R1_H1
	#ip route add 10.0.0.0/24 encap seg6 mode encap segs A2::2 dev R1_R2
	ip route add 10.0.0.0/24 nexthop encap seg6 mode encap segs A2::2 dev R1_R2 nexthop encap seg6 mode encap segs A2::2 dev R1_R3
'

#R2
ip link add name R2_H2 type veth peer name H2_R2
ip link add name R2_R3 type veth peer name R3_R2
ip link set R2_H2 netns R2
ip link set R2_R1 netns R2
ip link set R2_R3 netns R2
ip netns exec R2 bash -c \
'
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R2_H2.seg6_enabled=1
	sysctl net.ipv6.conf.R2_R1.seg6_enabled=1

	sysctl net.ipv4.conf.all.rp_filter=0
	sysctl net.ipv4.conf.default.rp_filter=0

	ip link set lo up
	ip link set R2_R1 up
	ip link set R2_H2 up
	ip link set R2_R3 up

	ip addr add 10.0.0.254/24 dev R2_H2
	ip addr add 2001:db8:1::21/64 dev R2_R1
	ip addr add 2001:db8:3::23/64 dev R2_R3
	ip addr add fe80::2/64 dev R2_R1
	ip addr add fe80::2/64 dev R2_H2
	ip addr add fe80::2/64 dev R2_R3


	ip -6 route add A2::2 encap seg6local action End.DX4 nh4 10.0.0.1 count dev R2_H2

	ip -6 route add A1::1 via fe80::1 dev R2_R1
	ip -6 route add A3::3 via fe80::3 dev R2_R3
	ip route add 192.168.0.0/24 encap seg6 mode encap segs A3::3,A1::1 dev R2_R1
'

#R3
ip link set R3_R1 netns R3
ip link set R3_R2 netns R3
ip netns exec R3 bash -c \
'
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R3_R1.seg6_enabled=1
	sysctl net.ipv6.conf.R3_R2.seg6_enabled=1

	ip addr add A3::3 dev lo
	ip addr add 2001:db8:2::31/64 dev R3_R1
	ip addr add 2001:db8:3::32/64 dev R3_R2
	ip addr add fe80::3/64 dev R3_R2
	ip addr add fe80::3/64 dev R3_R1

	ip link set lo up
	ip link set R3_R1 up
	ip link set R3_R2 up

	ip -6 route add A2::2 via fe80::2 dev R3_R2
	ip -6 route add A1::1 via fe80::1 dev R3_R1
'

#H2
ip link set H2_R2 netns H2
ip netns exec H2 bash -c \
'
	ip link set lo up
	ip link set H2_R2 up

	ip addr add 10.0.0.1/24 dev H2_R2
	ip route add 192.168.0.0/24 via 10.0.0.254
'